var rongZhao = {};

rongZhao.tooth = {
	
	getByClass: function(oParent, sClass)
	{
		var aEle=oParent.getElementsByTagName('*');
		var aResult=[];
		var re=new RegExp('\\b'+sClass+'\\b', 'i');
		var i=0;
		
		for(i=0;i<aEle.length;i++)
		{
			//if(aEle[i].className==sClass)
			//if(aEle[i].className.search(sClass)!=-1)
			if(re.test(aEle[i].className))
			{
				aResult.push(aEle[i]);
			}
		}
		
		return aResult;
	},
	
	getStyle: function(obj, attr)
	{
		if(obj.currentStyle)
		{
			return obj.currentStyle[attr];
		}
		else
		{
			return getComputedStyle(obj, false)[attr];
		}
	},
	
	myAddEvent: function(obj, sEvent, fn)
	{
		if(obj.attachEvent)
		{
			obj.attachEvent('on'+sEvent, fn);
		}
		else
		{
			obj.addEventListener(sEvent, fn, false);
		}
	},
	
	setCookie: function(name, value, iDay)
	{
		var oDate=new Date();
		
		oDate.setDate(oDate.getDate()+iDay);
		
		document.cookie = name + '=' + value + ';expires=' + oDate;
	},
	
	getCookie: function(name)
	{
		//'username=abc; password=123456; aaa=123; bbb=4r4er'
		var arr = document.cookie.split('; ');
		var i = 0;
		
		//arr->['username=abc', 'password=123456', ...]
		
		for(i=0;i<arr.length;i++)
		{
			//arr2->['username', 'abc']
			var arr2 = arr[i].split('=');
			
			if(arr2[0] == name)
			{
				return arr2[1];
			}
		}
		return '';
	},
	
	removeCookie: function(name)
	{
		setCookie(name, '1', -1);
	},
	
	 ajax: function(url, fnSucc, fnFaild)
	{
		//1.创建Ajax对象
		var oAjax = null;
		
		if(window.XMLHttpRequest)
		{
			oAjax = new XMLHttpRequest();
		}
		else
		{
			oAjax = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		//2.连接服务器
		oAjax.open('GET', url, true);
		
		//3.发送请求
		oAjax.send();
		
		//4.接收服务器的返回
		oAjax.onreadystatechange = function ()
		{
			if(oAjax.readyState == 4)	//完成
			{
				if(oAjax.status == 200)	//成功
				{
					fnSucc(oAjax.responseText);
				}
				else
				{
					if(fnFaild)
						fnFaild(oAjax.status);
				}
			}
		}
	},
	
	resizeImg: function(img,iwidth,iheight)
	{
		var _img = new Image();
		_img.src = img.src;
		if(_img.width > _img.height)
		{
		img.width = (_img.width > iwidth) ? iwidth : _img.width;
		img.height = (_img.height / _img.width) * img.width;
		}
		else if(_img.width < _img.height)
		{
		img.height = (_img.height > iheight) ? iheight : _img.height;
		img.width = (_img.width / _img.height) * img.height ;
		}
		else
		{
		img.height = (_img.height > iheight) ? iheight : _img.height;
		img.width = (_img.width > iwidth) ? iwidth : _img.width;
		}
	},
	
	resizeImgA: function(img,boxWidth,boxHeight)
	{
		var imgWidth = img.width;
		var imgHeight = img.height;
		//比较imgBox的长宽比与img的长宽比大小
		if((boxWidth/boxHeight)>=(imgWidth/imgHeight))
		{
			//重新设置img的width和height
			var xwidth = (boxHeight*imgWidth)/imgHeight;
			img.width = (boxHeight*imgWidth)/imgHeight;
			img.height = boxHeight;
		   
		}
		else
		{
			//重新设置img的width和height
			img.width = boxWidth;
			img.height = (boxWidth*imgHeight)/imgWidth;
		}
	}
	
};

rongZhao.ui = {
	
	startMove: function(obj, json, fn)
	{
		clearInterval(obj.timer);
		obj.timer=setInterval(function (){
			var bStop=true;		//这一次运动就结束了——所有的值都到达了
			for(var attr in json)
			{
				//1.取当前的值
				var iCur=0;
				
				if(attr=='opacity')
				{
					iCur=parseInt(parseFloat(rongZhao.tooth.getStyle(obj, attr))*100);
				}
				else
				{
					iCur=parseInt(rongZhao.tooth.getStyle(obj, attr));
				}
				
				//2.算速度
				var iSpeed=(json[attr]-iCur)/8;
				iSpeed=iSpeed>0?Math.ceil(iSpeed):Math.floor(iSpeed);
				
				//3.检测停止
				if(iCur!=json[attr])
				{
					bStop=false;
				}
				
				if(attr=='opacity')
				{
					obj.style.filter='alpha(opacity:'+(iCur+iSpeed)+')';
					obj.style.opacity=(iCur+iSpeed)/100;
				}
				else
				{
					obj.style[attr]=iCur+iSpeed+'px';
				}
			}
			
			if(bStop)
			{
				clearInterval(obj.timer);
				
				if(fn)
				{
					fn();
				}
			}
		}, 30)
	},
	
	textChange:function(obj,str)
	{
		obj.onfocus = function()
		{
			if(this.value == str)
			{
				this.value ="";	
			}
		}
		obj.onblur = function()
		{
			if(this.value == "")
			{
				this.value = str;	
			}
		}
		
	}
	
};


